#include <my_header.h>
#include "factory.h"
/* Usage:  */
enum operation{
    //0~10特殊操作，错误返回之类的
    //11~20目录操作
    //21~30文件操作
    //31~40SQL操作
    //一些名字加了下划线是因为某一部分单独是关键字

    return_result=2,

    cd_der=11,
    ls_der=12,
    pwd_der=13,
    mkdir_der=14,
    rmdir_der=15,

    puts_file=21,
    file_hash=22,
    file_name=23,
    cur_size=24,
    gets_file=25,
    remove_file=26,
    dont_send=27,
    go_send=28,
    file_txt=29,

    salt=31,
    register_der=32,
    login=33
};
//维护的栈结构  要执行的命令  参数    输出给客户端的字符串 当前用户名  是否登录    pwd返回         ls返回                                                          
int dealOrder(MYSQL* mysql,int netfd,Link_list_t* catalog,int type,char* contxt,char* returnStr,char* user,int* login_flag,char* stackPath,char* ls_result,char* client_ip){
    char order[1024];
    strncpy(order,contxt,sizeof(order)-1);
    switch(type){
    case register_der:{
                          char* user_name = strtok(order,"|");
                          char* password = strtok(NULL,"|");
                          int ret = register_user(mysql,user_name,password);
                          if(ret == 0){
                              strcpy(returnStr,"register_sucess\n");
                              log_client_action(client_ip,"client_register_success");
                          }else{
                              strcpy(returnStr,"register_failed\n");
                              log_client_action(client_ip,"client_register_failed");
                          }
                          break;      
                      }
    case login:{
                   char* user_name = strtok(order,"|");
                   char* password = strtok(NULL,"|");
                   int ret = log_on(mysql,user_name,password,returnStr);
                   if(ret == 0){
                       log_client_action(client_ip,"client_log_on_sucess");
                   } else if(ret == -2){
                       log_client_action(client_ip,"client_log_on_failed_AccountNotFound");
                       break;
                   }else if(ret == -3){
                       log_client_action(client_ip,"client_log_on_failed_AccountNotFound");
                       break;
                   }
                   *login_flag = 1;
                   strcpy(user,user_name);
                   break;
               }
    case mkdir_der:{
                       char log_value[1024] = {0};
                       if(*login_flag == 0){break;}
                       int ret =  mkdir_order(mysql,contxt,user,catalog);
                       pwd_order(catalog,mysql,stackPath);
                       if(ret == 0){
                           sprintf(returnStr,"mkdir_%s_success\n%s-NetDisk:~%s$ ",contxt,user,stackPath);

                           sprintf(log_value,"%s__client_mkdir_%s_sucess",client_ip,contxt);
                           log_client_action(client_ip,log_value);
                       }else{
                           sprintf(returnStr,"mkdir_%s_failed\n%s-NetDisk:~%s$ ",contxt,user,stackPath);

                           sprintf(log_value,"%s__client_mkdir_%s_failed",client_ip,contxt);
                           log_client_action(client_ip,log_value);
                       }
                       break;
                   }
    case cd_der:{
                    char log_value[1024] = {0};
                    if(*login_flag == 0){break;}
                    int ret = cd_order(mysql,user,contxt,catalog);
                    pwd_order(catalog,mysql,stackPath);
                    if(ret == -1){
                        sprintf(returnStr,"cd__%s_failed\n%s-NetDisk:~%s$ ",contxt,user,stackPath);

                        sprintf(log_value,"%s__client_cd_%s_failed",client_ip,contxt);
                        log_client_action(client_ip,log_value);
                    }else if(ret == 0){
                        sprintf(returnStr,"%s-NetDisk:~%s$ ",user,stackPath);

                        sprintf(log_value,"%s__client_cd_%s_sucess",client_ip,contxt);
                        log_client_action(client_ip,log_value);
                    }
                    break;
                }
    case pwd_der:{
                     if(*login_flag == 0){break;}
                     pwd_order(catalog,mysql,stackPath);
                     sprintf(returnStr,"%s\n%s-NetDisk:~%s$ ",stackPath,user,stackPath);

                     char log_value[1024] = {0};
                     sprintf(log_value,"%s__client_pwd_sucess -- path: %s",client_ip,stackPath);
                     log_client_action(client_ip,log_value);
                     break;
                 }
    case ls_der:{
                    if(*login_flag == 0){break;}
                    pwd_order(catalog,mysql,stackPath);
                    ls_order(mysql,user,catalog,ls_result);
                    sprintf(returnStr,"%s\n%s-NetDisk:~%s$ ",ls_result,user,stackPath);
                    log_client_action(client_ip,"client_ls_success");
                    printf("%s\n",ls_result);
                    break;
                }
    case rmdir_der:{
                       char log_value[1024] = {0};
                       if(*login_flag == 0){break;}
                       int ret = rmdir_order(mysql,user,contxt,catalog);
                       if(ret == 0){
                           pwd_order(catalog,mysql,stackPath);
                           printf("删除目录%s成功\n",contxt);
                           sprintf(returnStr,"%s-NetDisk:~%s$ ",user,stackPath);

                           sprintf(log_value,"%s__client_rmdir_%s_sucess",client_ip,contxt);
                           log_client_action(client_ip,log_value);
                       }else{
                           sprintf(returnStr,"rmdir_%s_faild\n%s-NetDisk:~%s$ ",contxt,user,stackPath);

                           sprintf(log_value,"%s__client_rmdir_%s_failed",client_ip,contxt);
                           log_client_action(client_ip,log_value);

                       }
                       break;
                   }
    case puts_file:{
                        printf("222\n");
                       recv_file(netfd,mysql,catalog, user);
                       printf("1111\n");
                       break;
                   }
    case gets_file:{
                       push_file(netfd,contxt,user,mysql,catalog);
                       break;
                   }
    }
    return 0;
}
